package com.sxpi.zixun.service.chapterlesson;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.ChapterLessonBo;
import com.sxpi.zixun.mapper.ChapterLessonMapper;
import com.sxpi.zixun.pojo.ChapterLesson;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/9 8:51
 */
@Service
public class IChapterLessonServiceImpl extends ServiceImpl<ChapterLessonMapper, ChapterLesson> implements IChapterLessonService  {

    /**
     * 根据课程查询对应的章信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterLessonBo> findChapterList(Long courseId) {
        LambdaQueryWrapper<ChapterLesson> chapterLessonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chapterLessonLambdaQueryWrapper.eq(ChapterLesson::getCourseid,courseId);
        chapterLessonLambdaQueryWrapper.eq(ChapterLesson::getPid,0);
        // 删除以后，再次调用接口，把删除的放到下面去（前端）
        chapterLessonLambdaQueryWrapper.orderByAsc(ChapterLesson::getIsdelete,ChapterLesson::getSorted);
        List<ChapterLesson> list = this.list(chapterLessonLambdaQueryWrapper);
        return tranferListBo(list,ChapterLessonBo.class);
    }

    /**
     * 根据章ID查询对应节信息
     * @param chapterId
     * @return
     */
    @Override
    public List<ChapterLessonBo> findLessonList(Long chapterId) {
        LambdaQueryWrapper<ChapterLesson> chapterLessonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chapterLessonLambdaQueryWrapper.eq(ChapterLesson::getPid,chapterId);
        chapterLessonLambdaQueryWrapper.orderByAsc(ChapterLesson::getIsdelete,ChapterLesson::getSorted);
        List<ChapterLesson> list = this.list(chapterLessonLambdaQueryWrapper);
        return tranferListBo(list,ChapterLessonBo.class);
    }

    /**
     * 根据Id查询章节管理明细信息
     * @param id
     * @return
     */
    @Override
    public ChapterLessonBo getChapterLessonById(Long id) {
        ChapterLesson chapterLesson = this.getById(id);
        return tranferBo(chapterLesson,ChapterLessonBo.class);
    }

    /**
     * 保存&修改章节管理
     * @param chapterLesson
     * @return
     */
    @Override
    public ChapterLessonBo saveUpdateChapterLesson(ChapterLesson chapterLesson) {
        boolean flag = this.saveOrUpdate(chapterLesson);
        return flag ? tranferBo(chapterLesson,ChapterLessonBo.class) : null;
    }

    /**
     * 根据id删除章节管理
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteChapterLessonById(Long opid) {
        ChapterLesson chapterLesson = this.getById(opid);
        if(chapterLesson != null){
            ChapterLesson chapterLessonNew = new ChapterLesson();
            chapterLessonNew.setId(opid);
            chapterLessonNew.setIsdelete(1);
            this.saveOrUpdate(chapterLessonNew);
            return 1;
        }
        return 0;
    }

    /**
     * 物理删除章节信息
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeChapterLessonById(Long id) {
        ChapterLesson chapterLesson = this.getById(id);
        // 这里删除的是节
        if(chapterLesson != null && !chapterLesson.getPid().equals(0L)){
            // 删除的节
            boolean flag = this.removeById(chapterLesson.getId());
            return  flag ? 1 : 0;
        }else{
            // 删除章
            boolean b = this.removeById(chapterLesson.getId());
            // 查询章下面所有的节
            List<ChapterLessonBo> lessonList = findLessonList(chapterLesson.getId());
            // 删除章下面的节
            if(!CollectionUtils.isEmpty(lessonList)){
                List<Long> lessonIds = lessonList.stream().map(chapter -> {
                    return chapter.getId();
                }).collect(Collectors.toList());
                this.removeByIds(lessonIds);
            }
            return b ? 1 : 0;
        }
    }

    /**
     * 恢复删除
     * @param opid
     * @return
     */
    @Override
    public int recoverChapterLessonById(String opid) {
        ChapterLesson chapterLesson = new ChapterLesson();
        chapterLesson.setId(new Long(opid));
        chapterLesson.setIsdelete(0);
        return this.updateById(chapterLesson) ? 1 : 0;
    }

}
